﻿#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowTitle("Thread");

    m_pThread = new MyThread();

    ui->progressBar->setValue(0);
//    ui->progressBar->setRange(0, 100);

    connect(ui->startBtn, &QPushButton::clicked, this, &Widget::slotStartImport);

    connect(m_pThread, &MyThread::destroyed, this, &Widget::wait);
    //刷新进度条
    connect(m_pThread, &MyThread::sigImportRefresh, this, &Widget::slotRefreshValue);
}

Widget::~Widget()
{
    if(m_pThread->isRunning())
    {
        m_pThread->quit();
        m_pThread->wait();
    }
    delete m_pThread;
    delete ui;
}

void Widget::slotStartImport()
{
    qWarning() << "main Thread id:" << QThread::currentThread();
    m_pThread->start();
}

void Widget::wait()
{
    if(!m_pThread->isRunning())
    {
        return;
    }

    m_pThread->quit();
    m_pThread->wait();
}

void Widget::slotRefreshValue(int iValue)
{
    ui->progressBar->setValue(iValue);
}
